var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    currentTab: 0,
    showModal: false,
    "bnrUrl": [{
      "url": "../../images/2.jpg"
    }, {
        "url": "../../images/2.jpg"
    }, {
        "url": "../../images/2.jpg"
    }],
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [{
    }],
    covers: [{
    
    }]
  },
  navigate() {
    ////使用微信内置地图查看标记点位置，并进行导航
    var that = this
    var latitude = that.data.latitude
    var longitude = that.data.longitude
    var address = that.data.address
    var detailaddress = that.data.detailaddress
        wx.openLocation({
          latitude: Number(latitude), // 纬度，范围为-90~90，负数表示南纬
          longitude: Number(longitude), // 经度，范围为-180~180，负数表示西经
          scale: 15, // 缩放比例
          name: address,
          address: detailaddress
        })
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => { },
    });
  },
  preventTouchMove: function () {

  },

  go: function () {
    this.setData({
      showModal: false
    })
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    //设置语言0:中文，1：英文
    var that = this
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Attendees Guide'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '参会指南'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //加载会议地址
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //exhibitionid = '33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var cururl = app.globalData.picoaddress + '/attendee/indexpage/exhaddress'
      var m = {}
      m["v"] = '1.0.0'
      m["client"] = 'ios'
      m["timestamp"] = timestamp
      m["lan"] = 'cn'
      m["exhibitionid"] = exhibitionid
      //联系人id
      m["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      m["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: cururl,
        method: 'post',
        data: m,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          var address = curdata.address
          var detailaddress = curdata.detailaddress
          var location = curdata.location
          var latitude = location.lat
          var longitude = location.lng
          console.log('address=' + address)
          console.log('detailaddress=' + detailaddress)
          console.log('location='+location)
          console.log('latitude=' + latitude)
          console.log('latitude=' + latitude)
          var markers =[]
          markers.push('id','1')
          markers.push('latitude', latitude)
          markers.push('longitude', longitude)
          markers.push('name', address)
          var covers = []
          covers.push('latitude', latitude)
          covers.push('longitude', longitude)
          that.setData({
            address: address,
            detailaddress: detailaddress,
            latitude: latitude,
            longitude: longitude,
            markers: markers,
            covers: covers
          })
        }, fail: res => {
          console.log(res, "error")
        }
      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }


  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function () {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude: 23.10229,
        longitude: 113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function () {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude: 23.10229,
        longitude: 113.3345211,
      }, {
        latitude: 23.00229,
        longitude: 113.3345211,
      }]
    })
  }
})